Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Sometimes you have an abstract parent data object with multiple child data objects, for example:
A model can have a JSON field which is either one of these data objects:
You can then store either a
CdRecordConfig
or aVinylRecord
in theconfig
field:When a data object class is abstract and used as an Eloquent cast then this feature will work out of the box.
The child data object value of the model will be stored in the database as a JSON string with the class name as the key:
When retrieving the model, the data object will be instantiated based on the
type
key in the JSON string.Abstract data class morphs
By default, the
type
key in the JSON string will be the fully qualified class name of the child data object. This can break your application quite easily when you refactor your code. To prevent this, you can add a morph map like with Eloquent models. Within yourAppServiceProvivder
you can add the following mapping:Why no DataCollection support?
At this point in type a
DataCollection
can only be of one type only, so no use to make a cast for abstract child classes for now.